<?php 
$pPageIsPublic = false;
include '../_common.php';
$_SESSION["m1"]="suggestion";
$_SESSION["m2"]="bycountry";

if ($_POST['countryId']) {	
	if ($_POST['action'] == 'submit'){		
	}else{
		return fetchRegions($_POST['countryId']);
	}
}

if ($_POST['action'] == 'submit') {
	$regions = $_POST['region'];
	$suggest    = $_POST['suggest'];
	$countryId = $_POST['countryId'];
	
	foreach($regions as $key => $region) {
		$tmp = new Region();
		if (! $tmp->loadByFilter('country.countryId='. $countryId 
							  .' AND '. $tmp ->gTable().'.regionId='. $region)) {
			continue;
		} else {
			$tmp ->suggest = $suggest[$key];
			$tmp ->update('suggest');
		}
	}
	return;
}

?>
<?php include("header.php"); ?>   
<script type="text/javascript">
$().ready(function() {

	
	$('input:checkbox.all').live('click', function() {
		$('ul#listin').find('input:checkbox').attr('checked', this.checked);
	});
	
	$('input[type="button"]').live('click', function() {
		var ids = "";
		var values = "";
		
		$('input:checkbox.check').each(function(){
			ids += "&region[]=" + this.name;
			values += "&suggest[]=" + (this.checked ? "1" : "0");         
		});
		
		$.ajax({
			type      : "POST",
			data   	  : "action=submit&countryId=" + $('select[name="countryId"]').val() + ids + values,
			success   : function(response) {
							Notify("Suggestion for this Country has been updated.", 0);	
						}
		});
	});
});

function loadList(id) {
	$.ajax({
		type      : "POST",
		data   	  : "countryId=" + id,
		success   : function(response) {
						$('div#list').html(response);	
					}
	});
}

</script> 
<?php include("sidebar.php"); ?>
<ul id="select-nav"   >
  <li class="noborder"  ><strong> Select a Country:</strong></li>
  <li class="noborder" >
<?php
	$tmp = new Country();
	$tmp ->addOrder($tmp ->gTable().'.title ASC');
	Generic::gSelect($tmp, '', '', '--- select a country --', 'width:210px;', 'onchange="return this.value ? loadList(this.value) : location.reload(true);"');
	unset($tmp);
?>  
  </li>
</ul>

<div id="list">
	<h4>Select a Country</h4> 
</div>
<div class="clear"></div>    
<?php include("footer.php"); ?>

<?php
	function fetchRegions($cId){
		$tmp = new Region();
		$tmp ->loadItems('#title ASC', '#countryId='. $cId);
		if (! $tmp ->rMore()) {	
			echo "<h4>Zero regions on this country.</h4>";
		} else { ?>
	<h2>Suggestions for Country</h2>
	<table id="gradient-style">
		<thead>
			<tr>
			 <th width="88%" >Region</th>
			 <th width="12%" >Suggest <input type="checkbox" class="all"   > </th>
			</tr>
		</thead>
	</table>
	<ul id="listin" >
<?php 		
			while ($region = $tmp ->rNext()) {
?>
		<li><table id="gradient-style" >
		  <tr>
		    <td width="88%"  ><?php echo $region ->title   ?></td>
		    <td width="12%" style="text-align: right; padding-right: 9px;"   >
		    <input class="check"  type="checkbox" id="region[]" name="<?php echo $region ->id  ?>"   <?php echo $region ->suggest ? 'checked="checked"' : ''?> /></td>
		  </tr>
		</table></li>			
<?php 				
			} ?>
	</ul>
	<div style="padding-top: 8px;text-align: right; letter-spacing: 6px;" >
		<input type="button" value="Save" name="btnSave"   />
	</div>			
<?php 			
		}
	}
?>
